//
//  LeContactListener.h
//  Leste
//
//  Created by 王乐 on 2022/1/24.
//

#ifndef LeContactListener_hpp
#define LeContactListener_hpp

#include <stdio.h>
#include <box2d/box2d.h>

#include "LeGameObject.h"
#include "LeBehaviour.h"

class LeContactListener : public b2ContactListener
{
    
public:
    LeContactListener(){};
    virtual ~LeContactListener(){};
    
void BeginContact(b2Contact* contact)
{
    uintptr_t iNameA = contact->GetFixtureA()->GetBody()->GetUserData().pointer;
    uintptr_t iNameB = contact->GetFixtureB()->GetBody()->GetUserData().pointer;

    LeGameObject *pGameObjectA = iNameA != 0 ? reinterpret_cast<LeGameObject*>(iNameA) : nullptr;
    LeGameObject *pGameObjectB = iNameB != 0 ? reinterpret_cast<LeGameObject*>(iNameB) : nullptr;
    
    if(pGameObjectA)
    {
        pGameObjectA->OnContactBegin(pGameObjectB);
    }
    if(pGameObjectB)
    {
        pGameObjectB->OnContactBegin(pGameObjectA);
    }
    
    printf("BeginContact\n");
}
 
void EndContact(b2Contact* contact)
{
    uintptr_t iNameA = contact->GetFixtureA()->GetBody()->GetUserData().pointer;
    uintptr_t iNameB = contact->GetFixtureB()->GetBody()->GetUserData().pointer;

    LeGameObject *pGameObjectA = iNameA != 0 ? reinterpret_cast<LeGameObject*>(iNameA) : nullptr;
    LeGameObject *pGameObjectB = iNameB != 0 ? reinterpret_cast<LeGameObject*>(iNameB) : nullptr;
    
    if(pGameObjectA)
    {
        pGameObjectA->OnContactEnd(pGameObjectB);
    }
    if(pGameObjectB)
    {
        pGameObjectB->OnContactEnd(pGameObjectA);
    }
    
    printf("EndContact\n");
}
 
void PreSolve(b2Contact* contact, const b2Manifold* oldManifold)
{
//    printf("PreSolve\n");
}
 
void PostSolve(b2Contact* contact, const b2ContactImpulse* impulse)
{
//    printf("PostSolve\n");
}
    
};
#endif /* LeContactListener_hpp */
